﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KMClient
{
    public partial class Login : Form
    {
        public static ClientRequest cRequst = new ClientRequest();

        public Login()
        {
            InitializeComponent();

            this.ClientSize = new System.Drawing.Size(290, 270);
            cRequst.Start();
            cRequst.dLogout += HLogout;
           
        }
         ~Login()
        {
            Application.Exit();
        }
        private void btReg_Click(object sender, EventArgs e)
        {
            if (tbRegPassword.Text.Trim()==""||tbRegPassword2.Text.Trim()=="")
            {
                 MessageBox.Show("用户名和密码不能为空！");
                return;
            }
            if (tbRegPassword.Text != tbRegPassword2.Text)
            {
                MessageBox.Show("两次密码输入不一致！");
                return;
            }
            Dictionary<string, string> msg = cRequst.Register(tbRegUserName.Text.Trim(), tbRegPassword.Text);
            if (msg["status"] == "true")
            {
                tbRegPassword.Text = "";
                tbRegPassword2.Text = "";
                tbRegUserName.Text = "";
            }
            MessageBox.Show(msg["message"]);

        }

        private void btLogin_Click(object sender, EventArgs e)
        {
            
            if (tbLoginUserName.Text.Trim() == "" ||tbLoginPassword.Text == "")
            {
                MessageBox.Show("帐号、密码不能为空！");
                return;
            }
            Dictionary<string, string> msg = cRequst.Login(tbLoginUserName.Text, tbLoginPassword.Text);
            if (msg["status"] == "true")
            {
                lbUserName.Text = tbLoginUserName.Text;
                tab1.Visible = false;
                tab2.Location = new Point(12, 12);
                tab2.Visible = true;
                this.trEndTime.Enabled = true;
                IsLogin = true;
            }
            else 
            { 
                 MessageBox.Show(msg["message"]);
            }
           
        }

        private void tbRegUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//如果输入的是回车键  
            {
                this.btReg_Click(sender, e);//触发button事件  
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {
           
        }


        private void btResetPassword_Click(object sender, EventArgs e)
        {
            //修改密码
            if (tbOldPassrod.Text== "" || tbNewPassword.Text=="")
            {
                MessageBox.Show("原密码、新密码不能为空！");
                return;
            }
            if (tbNewPassword.Text != tbNewPassword2.Text)
            {
                MessageBox.Show("新密码不一致！");
                return;
            }
            Dictionary<string, string> msg = cRequst.ResetPassword(tbOldPassrod.Text,tbNewPassword.Text);
            if (msg["status"] == "true")
            {
                tbNewPassword2.Text = tbNewPassword.Text = tbOldPassrod.Text = "";
            }
            
            MessageBox.Show(msg["message"]);
            

        }

       
        private void btRecharge_Click(object sender, EventArgs e)
        { //充值
            if (tbCDKEY.Text.Trim()=="")
            {
                MessageBox.Show("请输入充值CDKEY");
                return;
            }
            Dictionary<string, string> msg = cRequst.Recharge(tbCDKEY.Text.Trim());
            if (msg["status"] == "true")
            {
                tbCDKEY.Text = "";
            }
            MessageBox.Show(msg["message"]);
        }

        private void lbtLogout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            IsHeartLogout = false;
            cRequst.Logout();
        }
        bool IsLogin = false;
        bool IsHeartLogout = false;
        public void HLogout()
        {
            IsLogin = false;
            IsHeartLogout = true;
            cRequst.Logout();
        }
        private void trEndTime_Tick(object sender, EventArgs e)
        {
            if (!IsLogin)
            {
                trEndTime.Enabled = false;
                tab1.Visible = true;
                tab2.Visible = false;
                if (IsHeartLogout)
                {
                    MessageBox.Show(cRequst.HeartbeadtMessage);
                }
               
            }
                TimeSpan ts = cRequst.endTime.Subtract(cRequst.serverTime);
                if (ts < new TimeSpan(0, 0, 0, 0))
                {
                    lblEndTime.Text = "已经过期,请充值后继续使用";
                }
                else
                {
                    lblEndTime.Text = string.Format("{0}天{1}小时{2}分", ts.Days, ts.Hours, ts.Minutes);
                }
        }

        private void btRunScript_Click(object sender, EventArgs e)
        {
           var d= new testScriptMainFrom();
           d.Show();
        }

        private void tbNewPassword2_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//如果输入的是回车键  
            {
                this.btResetPassword_Click(sender, e);//触发button事件  
            }
        }

        private void tbCDKEY_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//如果输入的是回车键  
            {
                this.btRecharge_Click(sender, e);//触发button事件  
            }
        }

        private void tbLoginUserName_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//如果输入的是回车键  
            {
                this.btLogin_Click(sender, e);//触发button事件  
            }
        }

      
       


    }
}
